package com.example.wms;


import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.wms.domain.Rows;

import java.util.ArrayList;


/**
 * 列表适配器
 */

public class OrderRecyclerViewAdapter extends RecyclerView.Adapter<OrderRecyclerViewAdapter.ViewHolder> {


    private ArrayList<Rows> jsonArray;

    private LayoutInflater inflater;


    private OnLaunchClickListener launchOnClickListener;

    private OnItemClickListener itemOnClickListener;

    private OnItemLongClickListener longClickListener;


    public OrderRecyclerViewAdapter(ArrayList<Rows> rows, MainActivity mContext) {
        jsonArray = rows;
        inflater = mContext.getLayoutInflater();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = inflater.inflate(R.layout.item_dev_list, parent, false);

        return new ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
        Rows rows = jsonArray.get(position);
        holder.title.setText(rows.getProductName());
        holder.openorclose.setText(rows.getAuditFlag() == 1 ? "待出库" : "已完成");
        holder.time.setText(rows.getCount() + "# ");

        if (rows.getAuditFlag() != 1) {
            holder.allLaunch.setVisibility(View.INVISIBLE);
        }

        holder.allListMode.setOnClickListener(v -> {
            if (itemOnClickListener != null) {
                itemOnClickListener.onClick(position);
            }
        });
        holder.allLaunch.setOnClickListener(v -> {
            RotateAnimation rotateAnimation = new RotateAnimation(0, 360, RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
            rotateAnimation.setDuration(2000);
            rotateAnimation.setFillAfter(true);
            holder.ivLaunch.startAnimation(rotateAnimation);
            if (launchOnClickListener != null) {
                launchOnClickListener.onClick(position, rows);
            }
        });
    }


    @Override
    public int getItemCount() {
        return jsonArray.size();
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        LinearLayout allListMode;
        LinearLayout allLaunch;
        ImageView ivLaunch;

        TextView title; //标题
        TextView openorclose; //开关与否
        TextView time; //开关与否


        ViewHolder(View view) {
            super(view);
            ivLaunch = view.findViewById(R.id.ivLaunch);
            allListMode = view.findViewById(R.id.allListMode);
            allLaunch = view.findViewById(R.id.allLaunch);

            title = view.findViewById(R.id.title);
            openorclose = view.findViewById(R.id.openorclose);
            time = view.findViewById(R.id.time);

        }
    }


    public void setOnItemClickListener(OrderRecyclerViewAdapter.OnItemClickListener itemOnClickListener) {
        this.itemOnClickListener = itemOnClickListener;
    }


    public void setOnLaunchClickListener(OrderRecyclerViewAdapter.OnLaunchClickListener itemOnClickListener) {
        this.launchOnClickListener = itemOnClickListener;
    }

    public void setlongClickListener(OrderRecyclerViewAdapter.OnItemLongClickListener longClickListener) {
        this.longClickListener = longClickListener;
    }


    public interface OnLaunchClickListener {
        void onClick(int position, Rows rows);
    }

    public interface OnItemClickListener {
        void onClick(int position);
    }

    public interface OnItemLongClickListener {
        void onClick(int position);
    }


}

